<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>商品一览页面</title>
<link href="css/jimu.css" type="text/css" rel="stylesheet">
<style type="text/css">
	ul.validateError{
		list-style:none;
		color:red;
		margin:0px;
		margin:bottom:5px;
	}
</style>
<script type="text/javascript">
		var xhr;
	function checkName(obj){
		//alert(obj.value);
		//通过ajax访问服务器端，判断商品是否已经存在
		if(window.ActiveXOject){
			//当浏览器为IE5以下时，要使用ActiveX方式创建对象
			xhr=new ActionXObject("Microsoft.XMLHTTP");
			//alert("Microsoft类型浏览器");
		}else if(window.XMLHttpRequest){
			xhr=new XMLHttpRequest();
			//alert("XMLHttpRequest类型浏览器");
		}else{
			//alert("浏览器不支持Ajax技术");			
			return;
		}
		//3.访问服务器端
		//3.1追加回调函数
		xhr.onreadystatechange=callback;
		//3.2打开要访问的服务器资源
		//xhr.open("get","checkProdName.action?prodname="+obj.value);
		
		//用post方法打开服务器端
		xhr.open("post","checkProdName.action");
		//3.3发送对服务器端的访问请求
		//xhr.setRequestHeader("If-Modified-Since","0");
		xhr.setRequestHeader("If-Modified-Since","0");
		xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");//设置为中文
		xhr.send("prodname="+obj.value);		
	}
	
	function callback(){
		//alert("回调函数执行");
		//5.根据服务器返回结果，处理客户端
		//如果服务器端执行完毕
		 if(xhr.readyState == 4){
			//如果结果为执行成功
			if(xhr.status == 200){
				var ret=xhr.responseText;
				//alert(ret);
				//indexOf没有就是-1，有就从0开始
				if(ret.indexOf("Yes") != -1){
					//alert("该商品已经存在，不能再次输入");
					var divObj=document.getElementById("checkProdName");
					divObj.innerHTML="该商品已经存在，不能再次输入";
				} else {
					var divObj = document.getElementById("checkProdName");
					divObj.innerHTML="";
				}

			}else if(xhr.status == 404) {
				alert(404);
			}
		} 	
		
	}
	function checkSubmit(){
		var divObj=document.getElementById("checkProdName");
		//如果商品名的重复检查结果的区域内不为空，说明有提示信息
		//数据不能提交
		if(divObj.innerHTML){
			return false;
		}
		return true;		
	}
</script>
</head>
<body>
<!-- 头部的包含文件 -->
<%@include file="../common/head.jsp" %>
<table class="main">
	<tr>
		<td style="width:180px; vertical-align:top;">
		<%@ include file="../common/menu.jsp" %>
		</td>
		<td style="vertical-align:top;padding:20px;">
			<p>
				<s:text name="promt"/><!-- 使用国际化完成 -->
			</p>
			<s:div id="checkProdName" cssStyle="color:red"></s:div>
			<s:form action="addProd.action" method="post" onsubmit="return checkSubmit();" enctype="multipart/form-data">	
			
					<s:textfield name="product.productname" key="prodname" onblur="checkName(this)"/>
					
					<%--  <s:doubleselect 
					 	doubleList="supMap.get(top.supertypeid)"
						doubleListKey="subtypeid" 
						doubleListValue="subtypename"
						list="superList" 
						listKey="supertypeid" 
						listValue="typename"
						doubleName="product.subtype.subtypeid"
						name="product.subtype.supertype.supertypeid" 
						
						cssStyle="width:75px" key="prodtype" /> --%>
						
						<s:doubleselect 
						label="类型选择" cssStyle="width:100px"
						
						list="superList"
						name="product.subtype.supertype.supertypeid"
						listKey="supertypeid"
						listValue="typename"
						
						doubleList="subtypes" 
						doubleName="product.subtype.subtypeid"
						doubleListKey="subtypeid"
						doubleListValue="subtypename"
						
						/>

						<s:file name="photo" label="商品图片"/>
						
						<s:textfield key="prodp" name="product.price"/>
						
						<s:textfield key="prodnp" name="product.nowprice"/>
	
						<s:textfield key="prodn" name="product.productNum"/>
						
						<s:set name="host" value="#{1:'是',0:'否'}"/>
                        <s:radio list="#host" listKey="key" listValue="value" name="product.isHost" label="是否热销"/>
						<s:submit value="提交"/>
						<s:reset value="重置"/>
			</s:form>
	</td>
	</tr>
	</table>


<%@include file="../common/tail.jsp" %>
</body></html>